package com.tarena.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.tarena.bo.DeptDao;
import com.tarena.bo.EmpDao;
import com.tarena.bo.UserDao;
import com.tarena.controller.Action;
import com.tarena.vo.Dept;
import com.tarena.vo.Emp;
import com.tarena.vo.User;

/**
 * 
 * @更新员工信息页面请求处理方法
 * @author Wangpeihu
 * 
 */
public class UpdateEmpFormAction extends Action {
	private EmpDao ed = new EmpDao();
	private DeptDao dd = new DeptDao();
	private UserDao ud = new UserDao();

	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response) {
		try {
			String empId = request.getParameter("id");
			if (empId != null) {
				Integer id = Integer.parseInt(empId);
				Emp emp = ed.findEmpById(id);
				List<Dept> deptList = dd.findAllDept();
				request.setAttribute("deptList", deptList);
				request.setAttribute("emp", emp);
				HttpSession session = request.getSession();
				User user1 = (User) session.getAttribute("user_key");
				User user2 = ud.getUser(user1);
				request.setAttribute("user_detail", user2);
				RequestDispatcher rd = request
						.getRequestDispatcher("/WEB-INF/jsp/emp_update_form.jsp");
				rd.forward(request, response);
			} else {
				response.sendRedirect("emp_list");
			}
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
